import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/store'

// import Login from '../components/Login'
import Home from '../components/Home'
// import Welcome from '../components/Welcome'
import NotFound from '../components/NotFound'
const Login = () => import(/* webpackChunkName: "login_home_welcome_NotFound" */ '../components/Login')
// const Home = () => import(/* webpackChunkName: "login_home_welcome_NotFound" */ '../components/Home')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome_NotFound" */ '../components/Welcome')
// const NotFound = () => import(/* webpackChunkName: "login_home_welcome_NotFound" */ '../components/NotFound')

// import Users from '../components/user/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Role from '../components/power/Role.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Role = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Role.vue')

// import Commodity from '../components/goods/Commodity.vue'
// import Sales from '../components/goods/Sales.vue'
const Commodity = () => import(/* webpackChunkName: "goods" */ '../components/goods/Commodity.vue')
const Sales = () => import(/* webpackChunkName: "goods" */ '../components/goods/Sales.vue')

// import Staff from '../components/management/Staff.vue'
// import Department from '../components/management/Department.vue'
// import Supply from '../components/management/Supply.vue'
// import Supplier from '../components/management/Supplier.vue'
// import Cashier from '../components/management/Cashier.vue'
// import Warehouse from '../components/management/Warehouse.vue'
const Staff = () => import(/* webpackChunkName: "management" */ '../components/management/Staff.vue')
const Department = () => import(/* webpackChunkName: "management" */ '../components/management/Department.vue')
const Supply = () => import(/* webpackChunkName: "management" */ '../components/management/Supply.vue')
const Supplier = () => import(/* webpackChunkName: "management" */ '../components/management/Supplier.vue')
const Cashier = () => import(/* webpackChunkName: "management" */ '../components/management/Cashier.vue')
const Warehouse = () => import(/* webpackChunkName: "management" */ '../components/management/Warehouse.vue')

// import Report from '../components/statistics/Report.vue'
// import Pie from '../components/statistics/Pie.vue'
const Report = () => import(/* webpackChunkName: "report" */ '../components/statistics/Report.vue')
const Pie = () => import(/* webpackChunkName: "report" */ '../components/statistics/Pie.vue')

Vue.use(VueRouter)

const userRule = { path: '/users', component: Users }
const commodityRule = { path: '/Commodity', component: Commodity }
const salesRule = { path: '/Sales', component: Sales }
const rightsRule = { path: '/rights', component: Rights }
const staffRule = { path: '/Staff', component: Staff }
const deptRule = { path: '/Department', component: Department }
const supplyRule = { path: '/Supply', component: Supply }
const supplierRule = { path: '/Supplier', component: Supplier }
const warehouseRule = { path: '/Warehouse', component: Warehouse }
const cashierRule = { path: '/Cashier', component: Cashier }
const reportRule = { path: '/Report', component: Report }
const pieRule = { path: '/Pie', component: Pie }
const roleRule = { path: '/Role', component: Role }

const ruleMapping = {
  users: userRule,
  sales: salesRule,
  commodity: commodityRule,
  Rights: rightsRule,
  Role: roleRule,
  Staff: staffRule,
  Department: deptRule,
  Supplier: supplierRule,
  Supply: supplyRule,
  Warehouse: warehouseRule,
  Cashier: cashierRule,
  Report: reportRule,
  Pie: pieRule
}

const routes = [
  { path: '/', redirect: 'login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{ path: '/welcome', component: Welcome }
      // { path: '/users', component: Users },
      // { path: '/Commodity', component: Commodity },
      // { path: '/Sales', component: Sales },
      // { path: '/rights', component: Rights },
      // { path: '/Staff', component: Staff },
      // { path: '/Department', component: Department },
      // { path: '/Supply', component: Supply },
      // { path: '/Supplier', component: Supplier },
      // { path: '/Warehouse', component: Warehouse },
      // { path: '/Cashier', component: Cashier },
      // { path: '/Report', component: Report },
      // { path: '/Pie', component: Pie },
      // { path: '/Role', component: Role }
    ]
  },
  {
    path: '*',
    component: NotFound
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 后端指定接口验证了token的正确性
  if (!tokenStr) return next('/login')
  next()
})

export function initDynamicRoutes () {
  // 根据二级权限对路由规则动态添加
  const currentRoutes = router.options.routes
  // 遍历二级权限
  const rightList = store.state.rightList
  rightList.forEach(item => {
    item.children.forEach(item => {
      const temp = ruleMapping[item.path]
      currentRoutes[2].children.push(temp)
    })
  })
  router.addRoutes(currentRoutes)
}

export default router
